//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


using System;
using System.Text;
using System.Collections.Generic;
using EffiProz.Core.Lib;
using EffiProz.Core.Rights;
using EffiProz.Core.Indexes;
using EffiProz.Core.Errors;
using EffiProz.Core.Results;
using EffiProz.Core.Navigators;
using EffiProz.Core.DataTypes;
using EffiProz.Core.Store;
using EffiProz.Core.Persist;

namespace EffiProz.Core
{

    /**
     * Represents an SQL view or anonymous subquery (inline virtual table
     * descriptor) nested within an SQL statement. <p>
     *
     * Implements {@link org.hsqldb.lib.ObjectComparator ObjectComparator} to
     * provide the correct order of materialization for nested views / subqueries.
     *
     * @author boucherb@users
     * @author fredt@users
     */
    public class SubQuery : ObjectComparator
    {

        public int level;
        private bool _isCorrelated;
        private bool isExistsPredicate;
        private bool uniqueRows;
        private bool isUniquePredicate;
        public QueryExpression queryExpression;
        public Database database;
        private TableDerived table;
        public View view;
        public View parentView;

        // IN condition optimisation
        public Expression dataExpression;
        public bool isDataExpression;

        //
        public static SubQuery[] emptySubqueryArray = new SubQuery[] { };

        public SubQuery(Database database, int level, QueryExpression queryExpression,
                 int mode)
        {

            this.level = level;
            this.queryExpression = queryExpression;
            this.database = database;

            switch (mode)
            {

                case OpTypes.EXISTS:
                    isExistsPredicate = true;
                    break;

                case OpTypes.IN:
                    uniqueRows = true;

                    if (queryExpression != null)
                    {
                        queryExpression.setFullOrder();
                    }
                    break;

                case OpTypes.UNIQUE:
                    isUniquePredicate = true;

                    if (queryExpression != null)
                    {
                        queryExpression.setFullOrder();
                    }
                    break;
            }
        }

        public SubQuery(Database database, int level, QueryExpression queryExpression,
                 View view)
        {

            this.level = level;
            this.queryExpression = queryExpression;
            this.database = database;
            this.view = view;
        }

        public SubQuery(Database database, int level, Expression dataExpression,
                 int mode)
        {

            this.level = level;
            this.database = database;
            this.dataExpression = dataExpression;
            dataExpression.subQuery = this;
            isDataExpression = true;

            switch (mode)
            {

                case OpTypes.IN:
                    uniqueRows = true;
                    break;
            }
        }

        public bool isCorrelated()
        {
            return _isCorrelated;
        }

        public void setCorrelated()
        {
            _isCorrelated = true;
        }

        public TableDerived getTable()
        {
            return table;
        }

        public void prepareTable(Session session)
        {

            if (table != null)
            {
                return;
            }

            if (view == null)
            {
                table = TableUtil.newSubqueryTable(database, null);

                if (isDataExpression)
                {
                    TableUtil.setTableColumnsForSubquery(
                        table, dataExpression.nodeDataTypes,
                        uniqueRows || isUniquePredicate);
                }
                else
                {
                    TableUtil.setTableColumnsForSubquery(table, queryExpression,
                                                         uniqueRows
                                                         || isUniquePredicate);
                }
            }
            else
            {
                table = new TableDerived(database, view.getName(),
                                         TableBase.VIEW_TABLE, queryExpression);
                table.columnList = view.columnList;
                table.columnCount = table.columnList.size();

                table.createPrimaryKey();
            }
        }

        public void materialiseCorrelated(Session session)
        {

            if (_isCorrelated)
            {
                materialise(session);
            }
        }

        /**
         * Fills the table with a result set
         */
        public void materialise(Session session)
        {

            PersistentStore store;

            // table constructors
            if (isDataExpression)
            {
                store = session.sessionData.getSubqueryRowStore(table);

                dataExpression.insertValuesIntoSubqueryTable(session, store);

                return;
            }

            Result result = queryExpression.getResult(session,
                isExistsPredicate ? 1
                                  : 0);

            if (uniqueRows)
            {
                RowSetNavigatorData navigator =
                    ((RowSetNavigatorData)result.getNavigator());
                navigator.removeDuplicates();
            }

            store = session.sessionData.getSubqueryRowStore(table);

            table.insertResult(store, result);
            result.getNavigator().close();
        }

        public bool hasUniqueNotNullRows(Session session)
        {

            RowSetNavigatorData navigator = new RowSetNavigatorData(session,
                table);
            bool result = navigator.hasUniqueNotNullRows();

            return result;
        }

        public Object[] getValues(Session session)
        {

            RowIterator it = table.rowIterator(session);

            if (it.hasNext())
            {
                Row row = it.getNextRow();

                if (it.hasNext())
                {
                    throw Error.error(ErrorCode.X_21000);
                }

                return row.getData();
            }
            else
            {
                return new Object[table.getColumnCount()];
            }
        }

        public Object getValue(Session session)
        {

            Object[] data = getValues(session);

            return data[0];
        }

        /**
         * This results in the following sort order:
         *
         * view subqueries, then other subqueries
         *
         *    view subqueries:
         *        views sorted by creation order (earlier declaration first)
         *
         *    other subqueries:
         *        subqueries sorted by depth within select query (deep == higher level)
         *
         */
        public int compare(Object a, Object b)
        {

            SubQuery sqa = (SubQuery)a;
            SubQuery sqb = (SubQuery)b;

            if (sqa.parentView == null && sqb.parentView == null)
            {
                return sqb.level - sqa.level;
            }
            else if (sqa.parentView != null && sqb.parentView != null)
            {
                int ia = database.schemaManager.getTableIndex(sqa.parentView);
                int ib = database.schemaManager.getTableIndex(sqb.parentView);

                if (ia == -1)
                {
                    ia = database.schemaManager.getTables(
                        sqa.parentView.getSchemaName().name).size();
                }

                if (ib == -1)
                {
                    ib = database.schemaManager.getTables(
                        sqb.parentView.getSchemaName().name).size();
                }

                int diff = ia - ib;

                return diff == 0 ? sqb.level - sqa.level
                                 : diff;
            }
            else
            {
                return sqa.parentView == null ? 1
                                              : -1;
            }
        }
    }
}